"""
Case Type   : 防篡改
Case Name   : 禁止将日志消息附加到同名的现有日志文件
Create At   : 2022-03-17
Owner       : lwx1080719
Description :
    1.设置参数并重启数据库enableSeparationOfDuty=on
    2.执行查询
    3.恢复默认值
Expect      :
    1.成功
    2.成功
    3.成功
History     :
"""


import os
import unittest
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class ModifyCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.common = Common()
        self.default_value = self.common.show_param('enableSeparationOfDuty')

    def test_security(self):
        text = '-----step1：设置参数并重启数据库enableSeparationOfDuty=on; ' \
               'expect:成功-----'
        self.log.info(text)
        mod_msg = \
            self.primary_sh.execute_gsguc('set',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          'enableSeparationOfDuty=on')
        self.log.info(mod_msg)
        self.assertTrue(mod_msg, '执行失败:' + text)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)

        text = '-----step2：执行查询 expect:成功-----'
        self.log.info(text)
        sql_cmd = \
            self.primary_sh.execut_db_sql(
                f"select name,setting from pg_settings where "
                f"name = 'log_truncate_on_rotation' and setting != 'off';")
        self.log.info(sql_cmd)
        self.assertIn('0 rows', sql_cmd, '执行失败:' + text)

    def tearDown(self):
        text = '----step3:恢复默认值 expect:成功----'
        self.log.info(text)
        mod_msg1 = \
            self.primary_sh.execute_gsguc('set',
                                          self.constant.GSGUC_SUCCESS_MSG,
                                          f'enableSeparationOfDuty='
                                          f'{self.default_value}')
        self.log.info(mod_msg1)
        self.assertTrue(mod_msg1, '执行失败:' + text)
        restart_msg = self.primary_sh.restart_db_cluster()
        self.log.info(restart_msg)
        status = self.primary_sh.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status,
                        '执行失败:' + text)
        self.log.info(f'-----{os.path.basename(__file__)} end-----')
